package com.cah.game;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cah.library.UserFunctions;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class PendingGameListFragment extends Fragment
{
	private String userKey;
	MainActivity main;
	LinearLayout pendingGameList;
	JSONObject pendingGamesInfo;
	
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		main = (MainActivity)getActivity();
		userKey= main.getUserKey();
		main.setTitle("Current Pending Games");
		main.actionBar.setDisplayHomeAsUpEnabled(true);
		main.historyBackVisible = false;
		main.historyForwardVisible = false;
		main.invalidateOptionsMenu();
		
		pendingGameList = (LinearLayout)main.findViewById(R.id.pendingGamesLinearLayout);
		
		if(savedInstanceState != null)
		{
			try
			{
				pendingGamesInfo = new JSONObject(savedInstanceState.getString("pendingGamesInfo"));
			} catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			setPendingGamesInfo();
		}
		else
		{
			new PendingGamesProcess().execute();
		}
	}
	
	public void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		outState.putString("pendingGamesInfo", pendingGamesInfo.toString());
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.pending_games, container, false);
	}
	
	private class PendingGamesProcess extends AsyncTask<Void, Void, JSONObject>
	{
		ProgressDialog pdia;
		@Override
		protected void onPreExecute()
		{
			pdia = new ProgressDialog(main);
			pdia.setMessage("Loading game list...");
			pdia.show();
		}
		
		@Override
		protected JSONObject doInBackground(Void... params)
		{
			UserFunctions uf = new UserFunctions(userKey);
			return uf.fetchPendingGames();
		}
		
		protected void onPostExecute(JSONObject json)
		{
			pendingGamesInfo = json;
			setPendingGamesInfo();
			pdia.dismiss();
		}
		
	}
	
	private void setPendingGamesInfo()
	{
		try
		{
			if(pendingGamesInfo.getInt("success")==1)
			{
				JSONArray gameArray = pendingGamesInfo.getJSONArray("value");
				
				for(int i=0;i<gameArray.length();i++)
				{
					JSONObject game = gameArray.getJSONObject(i);
					final String gameKey = game.getString("gameKey");
					final String gameName = game.getString("gameName");
					
					TextView pendingGameTextView = new TextView(main);
					pendingGameTextView.setClickable(true);
					pendingGameTextView.setText(gameName);
					
					pendingGameTextView.setOnClickListener(new View.OnClickListener()
					{
						
						@Override
						public void onClick(View arg0)
						{
							main.startPendingGameFragment(gameKey);
						}
					});
					
					pendingGameList.addView(pendingGameTextView);
					
				}
			}
			else
			{
				Toast.makeText(main, "Something went wrong!", Toast.LENGTH_SHORT).show();
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}
}
